package com;

import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

class CreateQRcode {
	public static void main(String[] args) {
		//定义二维码宽
		int width=200;
		//高
		int height=200;
		//定义图片格式
		String format="png";
		//定义内容
		String content="如果我不是贾宝玉";
		
		
		//定义二维码参数
		HashMap hints=new HashMap();
		//设置编码
		hints.put(EncodeHintType.CHARACTER_SET,"utf-8");
		//设置容错等级，等级越高，容量越小
		hints.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.M);
		//设置边距
		hints.put(EncodeHintType.MARGIN,2);
		
		//生成矩阵
		try {
			 BitMatrix  bitMatrix=new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height,hints);
			 
			 //设置路径
			 Path file=new File("img.png").toPath();
			 
			 //输出图像
			 MatrixToImageWriter.writeToPath(bitMatrix, format, file);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		
		
		
	}

}
